package com.tianqu.tms.www.car.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.car.action.CarSearchTransferDetailAction;
import com.tianqu.tms.www.car.viewhelper.CarTransferViewHelper;

/**
 * @author zhangxueliang
 * 高级搜索车辆过户信息修改result controller
 */
public class CarSearchTransferInputResultController extends TmsController{
	private Logger logger = Logger.getLogger(CarSearchTransferInputResultController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			CarTransferViewHelper viewhelper = (CarTransferViewHelper)uc.getAttribute(CarSearchTransferDetailAction.CARTRANSFERVIEWHELPER);
			TransferAcceptedInfo transferAcceptedInfo = TransferAcceptedInfo.getInstance();
			transferAcceptedInfo = formToEntity(transferAcceptedInfo,viewhelper);
			TmsUserProfile profile = uc.getUserProfile();//更新会员id
			transferAcceptedInfo.setUpdateUserId(profile.getUniqueId());
			transferAcceptedInfo.updateTransferAcceptedInfoBySerial(transferAcceptedInfo);
			controller.commit();
			
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	logger.debug(this.getClass().getName() + "#execute was completed.");
		
	}

	/**
	 * viewhelper -> entity
	 * @param entity
	 * @param form
	 * @return
	 * @throws TmsDaoException 
	 */
	private TransferAcceptedInfo formToEntity(TransferAcceptedInfo entity,CarTransferViewHelper viewhelper) throws TmsDaoException{
		entity.setSerial(viewhelper.getSerial());
		entity.setBasicCarId(viewhelper.getBasicCarId());
		entity.setNewAgentAddress(viewhelper.getNewAgentAddress());
		entity.setNewAgentName(viewhelper.getNewAgentName());
		entity.setNewAgentOwnerIdCardNo(viewhelper.getNewAgentOwnerIdCardNo());
		entity.setNewAgentPhone(viewhelper.getNewAgentPhone());
		entity.setNewcarOwner(viewhelper.getNewcarOwner());
		entity.setNewcarOwnerIdCardNo(viewhelper.getNewcarOwnerIdCardNo());
		entity.setNewOwnerAddress(viewhelper.getNewOwnerAddress());
		entity.setNewOwnerOrgNo(viewhelper.getNewOwnerOrgNo());
		Codes code = Codes.getInstance();
		if (StringUtil.isNullOrBlank(viewhelper.getNewOwnerType())) {
			entity.setNewOwnerType(0);
		} else {
		entity.setNewOwnerType(Integer.parseInt(viewhelper.getNewOwnerType()));
		}
		if ("".equals(viewhelper.getPrice())) {
			entity.setPrice(0);
		} else {
			entity.setPrice(Integer.parseInt(StringUtil.removeDH(viewhelper.getPrice())));
		}
		entity.setGainType(Integer.parseInt(viewhelper.getGainType()));
		entity.setTransferType(Integer.parseInt(viewhelper.getTransferType()));
		entity.setTaxNo(viewhelper.getTaxNo());
		entity.setMailAddress(viewhelper.getMailAddress());
		if("市".equals(viewhelper.getMoveOutCity())){
			entity.setMoveOutCity("");
		}else{
			entity.setMoveOutCity(viewhelper.getMoveOutCity());
		}
		if("省".equals(viewhelper.getMoveOutProvince())){
			entity.setMoveOutProvince("");
		}else{
			entity.setMoveOutProvince(viewhelper.getMoveOutProvince());
		}		
		entity.setCarMarketIn(viewhelper.getCarMarketIn());
		entity.setNewOwnerPhone(viewhelper.getNewOwnerPhone());
		entity.setZipCode(viewhelper.getZipCode());
		entity.setNewOwnerTelPhone(viewhelper.getNewOwnerTelPhone());
		return entity;
	}
}
